﻿using Contracts;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace ConfigHandle
{
    public class ConfigManager : IConfigManager
    {
        private readonly string _configPath;
        private string json;

        public ConfigManager(string configPath)
        {
            _configPath = configPath;
        }

        public string GetJsonFile()
        {
            //StreamReader file = File.OpenText(_configPath);
            //return file.ReadToEnd();

            string json = string.Empty;
            using (FileStream fs = new FileStream(_configPath, FileMode.Open, System.IO.FileAccess.Read, FileShare.Read))
            {
                using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
                {
                    json = sr.ReadToEnd().ToString();
                }
            }
            return json;
        }

        public string GetJsonByKey(string key)
        {
            //StreamReader file = File.OpenText(_configPath);
            //JsonTextReader reader = new JsonTextReader(file);
            //while (reader.Read())
            //{
            //    Console.WriteLine(reader.TokenType + "\t\t" + reader.ValueType + "\t\t" + reader.Value);
            //    if (reader.TokenType.Equals(JsonToken.PropertyName) && reader.Value.Equals(key))
            //    {
            //        reader.Read();
            //        return reader.Value.ToString();  // 多层嵌套？
            //    }
            //}

            //StreamReader file = File.OpenText(_configPath);
            //JObject o = JObject.Parse(file.ReadToEnd());

            string json_key = null;
            using (FileStream fs = new FileStream(_configPath, FileMode.Open, System.IO.FileAccess.Read, FileShare.Read))
            {
                using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
                {
                    var json = sr.ReadToEnd().ToString();
                    JObject o = JObject.Parse(json);
                    json_key = o[key] == null ? null : o[key].ToString();
                }
            }

            return json_key;
        }

        public void UpdateJson(string json)
        {
            // save string to json
            using (FileStream fs = new FileStream(_configPath, FileMode.Create, System.IO.FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
                {
                    sw.WriteLine(json);
                }
            }
        }

        public bool UpdateJsonByKey(string key, JsonElement body)
        {
            var json_all = GetJsonFile();
            if (json_all != null)
            {
                JObject json_o = JObject.Parse(json_all);

                Console.WriteLine(json_o[key].Type);
                bool ret = true;
                switch (json_o[key].Type)
                {
                    case JTokenType.Integer:
                        json_o[key] = body.GetInt64();
                        break;
                    case JTokenType.Float:
                        json_o[key] = body.GetDouble();
                        break;
                    case JTokenType.Date:
                        json_o[key] = body.GetDateTime();
                        break;
                    case JTokenType.Object:
                        json_o[key] = JObject.Parse(System.Text.Json.JsonSerializer.Serialize(body));
                        break;
                    case JTokenType.String:
                        json_o[key] = body.GetString();
                        break;
                    default:
                        ret = false;
                        break;
                }
                UpdateJson(json_o.ToString());
                return ret;
            }
            return false;
        }
   
        

    }
}
